package com.krdz.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.krdz.bean.QrBean;
import com.krdz.bean.QrCondition;
import com.krdz.dao.QrDao;
import com.krdz.service.QrService;
import com.krdz.util.QrCodeCreateUtil;
import com.google.zxing.WriterException;
/**
 *二维码管理业务层实现
 * @author dinghao
 *
 * @date  2018年11月15日
 */
@Service("QrServiceImpl")
public class QrServiceImpl implements QrService{
	
	@Resource
	private QrDao qrDao;
	
	//查询二维码信息
	@Override
	public List<QrBean> findQrInfo(QrBean qr) {
	
		return qrDao.findQrInfo(qr);
	}
	
	//扫描二维码得到信息
	@Override
	public String scanQrCode(String path){
		try {
			InputStream inputStream=new FileInputStream(new File(path));
			
			try {
				String msg=QrCodeCreateUtil.readQrCode(inputStream);
				return msg;
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
		
			e.printStackTrace();
		}
		
		return "error";
	}
	//根据用户信息生成二维码
	@Override
	public String createUserQrCode(String content,String fileName) throws WriterException, IOException {
		
		try {
			int qrCodeSize=3000;	//二维码尺寸
			String imageFormat="JPEG";	//二维码类型
			String path="d:/QRbak/"+fileName+".jpg";
			OutputStream outputStream=new FileOutputStream(new File(path));
			if(QrCodeCreateUtil.createQrCode(outputStream,content,qrCodeSize,imageFormat)){
				return path;
			}
			
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} 
		
		return null;
	}
	//添加二维码入数据库
	@Override
	public boolean insertQr(QrBean qr) {
		
		return qrDao.insertQr(qr).equals(1)?true:false;
	}
	//查询二维码保存记录数
	@Override
	public Integer findQrCount() {
		
		return qrDao.findQrCount();
	}
	//删除二维码信息
	@Override
	public boolean deleteQr(QrBean qr) {
		
		return qrDao.deleteQr(qr).equals(1)?true:false;
	}

	@Override
	public List<QrCondition> findQrTypeCount() {
		List<QrCondition> li=new ArrayList<QrCondition>();
		String qrTextCount=qrDao.findQrToText().toString();
		String qrPosCount=qrDao.findQrToPos().toString();
		String qrWebCount=qrDao.findQrToWeb().toString();
		QrCondition qrCon=new QrCondition();		
		qrCon.setQrTextCount(qrTextCount);
		qrCon.setQrPosCount(qrPosCount);
		qrCon.setQrWebCount(qrWebCount);
		li.add(qrCon);
		return li;
	}


}
